﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Mobileterminal.Common;
using Mobileterminal.Dtos;
using Mobileterminal.Dtos.Yjb;
using Newtonsoft.Json;
using Sunny.UI;

namespace Mobileterminal.Forms.Yjb
{
    public partial class UpdateConsumableMaterial : UIEditForm
    {
        private ConsumableMaterialDTO app; // 使用你的DTO类
        public UpdateConsumableMaterial(string consumableId)
        {
            InitializeComponent();
            this.Load += async (sender, e) => await LoadDataAsync(consumableId);
        }




        private async void btnOK_Click_1(object sender, EventArgs e)
        {
            try
            {
                app.Consumabletype = uiTextBox7.Text;
                app.ConsumableCode = uiTextBox6.Text;
                app.ConsumableName = uiTextBox3.Text;
                app.Specification = uiTextBox1.Text;
                app.Unit = uiTextBox2.Text;
                app.UnitPrice = uiTextBox4.Text;
                app.Manufacturer = uiTextBox5.Text;
               

                // 序列化并创建请求内容
                var json = JsonConvert.SerializeObject(app);
                HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");

                // 发送PUT请求
                var responseJson = await HttpClientHelper.ClientAsync(
                    "PUT",
                    "https://localhost:7286/api/Yjb/UpdateConsum",  // 修正URL拼写
                    true,
                    httpContent
                );

                if (string.IsNullOrEmpty(responseJson))
                {
                    MessageBox.Show("服务器返回空响应，请检查服务端状态或请求数据。");
                    return;
                }

                // 反序列化响应
                var result = JsonConvert.DeserializeObject<ConsumableMaterialDTO>(responseJson);
                if (result != null)
                {
                    MessageBox.Show("修改成功");
                }
                else
                {
                    MessageBox.Show("反序列化失败，响应内容格式不正确。");
                }
            }
            catch (HttpRequestException ex)
            {
                MessageBox.Show($"网络请求失败：{ex.Message}");
            }
            catch (JsonException ex)
            {
                MessageBox.Show($"解析响应失败：{ex.Message}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生错误：{ex.Message}");
            }
        }
        private async Task LoadDataAsync(string consumableId)
        {
        
            var url = $"https://localhost:7224/api/Show/FantianConsum?ConsumableId={consumableId}";
            var json = HttpClientHelper.ClientAsync("GET", url);
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<List<ConsumableMaterialDTO>>>(await json);
            if (result != null && result.Data != null && result.Data.Count > 0)
            {
                app = result.Data[0];
                uiTextBox7.Text = app.Consumabletype;
                uiTextBox6.Text = app.ConsumableCode;
                uiTextBox3.Text = app.ConsumableName;
                uiTextBox1.Text = app.Specification;
                uiTextBox2.Text = app.Unit;
                uiTextBox4.Text = app.UnitPrice;
                uiTextBox5.Text = app.Manufacturer;

            }
        }
        public static List<ConsumableMaterialDTO> appsTypes = new List<ConsumableMaterialDTO>();

        private void UpdateConsumableMaterial_Load(object sender, EventArgs e)
        {

        }




















































        ////反填
        //private async Task LoadDataAsync(string consumableId)
        //{
        //    var url = $"https://localhost:7224/api/Show/Showhc?consumableId={consumableId}";
        //    var json = HttpClientHelper.ClientAsync("GET", url);
        //    var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<List<ConsumableMaterialDTO>>>(await json);
        //    if (result != null && result.Data != null && result.Data.Count > 0)
        //    {
        //        app = result.Data[0];
        //        uiTextBox7.Text =  app.Consumabletype;
        //        uiTextBox6.Text =   app.ConsumableCode;
        //        uiTextBox3.Text =   app.ConsumableName;
        //        uiTextBox1.Text =   app.Specification;
        //        uiTextBox2.Text =   app.Unit;
        //        uiTextBox5.Text =   app.Manufacturer;
        //        // 初始化分类下拉框（需要提前加载分类数据）
        //        LoadApparatusTypes(app.consumTypeId);
        //    }
        //}


        //public static List<ConsumTypeTree> appsTypes = new List<ConsumTypeTree>();
        ///// <summary>
        ///// 加载分类数据
        ///// </summary>
        ///// <param name="consumTypeId"></param>
        ///// <exception cref="NotImplementedException"></exception>
        //private async void LoadApparatusTypes(int consumTypeId)
        //{

        //    //获取分类对应的分类名称--下拉框

        //    var url = "https://localhost:7224/api/Show/ShowTree";


        //    var json = await HttpClientHelper.ClientAsync("GET", url);
        //    var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<List<ConsumTypeTree>>>(json);
        //    if (result.Code == 200)
        //    {
        //        uiComboBox1.DataSource = result.Data;
        //        uiComboBox1.DisplayMember = "ConsumTypeTypeName";
        //        uiComboBox1.ValueMember = "ConsumTypeId";
        //        uiComboBox1.SelectedValue = consumTypeId; //
        //    }

        //}



        ///// <summary>
        ///// 修改确定按钮点击事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private async void btnOK_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        // 更新数据模型

        //        uiComboBox1.Text = app.Consumabletype;
        //        uiTextBox6.Text = app.ConsumableCode;
        //        uiTextBox3.Text = app.ConsumableName;
        //        uiTextBox1.Text = app.Specification;
        //        uiTextBox2.Text = app.Unit;
        //        uiTextBox5.Text = app.Manufacturer;


        //        //app.Consumabletype = uiTextBox6.Text;
        //        //app.ConsumableCode = uiTextBox3.Text;
        //        //app.ConsumableName = uiTextBox1.Text;
        //        //app.Specification = uiTextBox2.Text;
        //        //app.Unit = uiTextBox5.Text;
        //        //app.Manufacturer = (int)uiComboBox1.SelectedValue;
        //        //app.Manufacturer = uiRadioButton1.Checked ? 1 : 2;

        //        // 序列化并创建请求内容
        //        var json = JsonConvert.SerializeObject(app);
        //        HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");

        //        // 发送PUT请求
        //        var responseJson = await HttpClientHelper.ClientAsync(
        //            "PUT",
        //            "https://localhost:7286/api/Yjb/UpdateConsum",  // 修正URL拼写
        //            true,
        //            httpContent
        //        );

        //        if (string.IsNullOrEmpty(responseJson))
        //        {
        //            MessageBox.Show("服务器返回空响应，请检查服务端状态或请求数据。");
        //            return;
        //        }

        //        // 反序列化响应
        //        var result = JsonConvert.DeserializeObject<ConsumableMaterialDTO>(responseJson);
        //        if (result != null)
        //        {
        //            MessageBox.Show("修改成功");
        //        }
        //        else
        //        {
        //            MessageBox.Show("反序列化失败，响应内容格式不正确。");
        //        }
        //    }
        //    catch (HttpRequestException ex)
        //    {
        //        MessageBox.Show($"网络请求失败：{ex.Message}");
        //    }
        //    catch (JsonException ex)
        //    {
        //        MessageBox.Show($"解析响应失败：{ex.Message}");
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show($"发生错误：{ex.Message}");
        //    }
        //}
        //private void uiLabel1_Click(object sender, EventArgs e)
        //{

        //}

        //private void UpdateConsumableMaterial_Load(object sender, EventArgs e)
        //{

        //}
    }
}
